package com.joyoung.sdk.utils;

import android.content.Context;
import android.util.Log;

import com.hiflying.smartlink.OnSmartLinkListener;
import com.hiflying.smartlink.v7.MulticastSmartLinker;

/**
 * Created by liuwei on 2016/7/11.
 */
public class JoyoungDMS {
    private MulticastSmartLinker mSnifferSmartLinker;
    private boolean mIsConncting = false;
    private ConfigUdpBroadcast configUdpBroadcast;
    private boolean isconncting = false;

    private static JoyoungDMS joyoungDMS = null;

    private JoyoungDMS() {

    }


    /***
     * 线程安全的单例
     *
     * @return JoyoungDMS
     */
    public synchronized static JoyoungDMS getInstance() {
        if (joyoungDMS == null) {
            joyoungDMS = new JoyoungDMS();
        }
        return joyoungDMS;
    }


    /***
     * 设备配网
     *
     * @param context 上下文
     * @param wifiName WiFi名
     * @param wifiPwd  wifi密码
     * @param onSmartLinkListener 配网回调
     */
    public void deviceDms(Context context, String wifiName, String wifiPwd, OnSmartLinkListener onSmartLinkListener) {
        mSnifferSmartLinker = MulticastSmartLinker.getInstance();
        if (!mIsConncting) {
            // 设置要配置的ssid 和pswd
            try {
                mSnifferSmartLinker.setOnSmartLinkListener(onSmartLinkListener);
                // 开始 smartLink
                mSnifferSmartLinker.start(context, wifiPwd, wifiName);
                mSnifferSmartLinker.setTimeoutPeriod(30000);// 设备超时时间，默认30秒
                mIsConncting = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /***
     * 设备发现
     *
     * @param context         上下文
     * @param connectCallBack 设备发现的回调
     */
    public void deviceDiscovery(Context context, ConfigUdpBroadcast.ConnectCallBack connectCallBack) {
        configUdpBroadcast = new ConfigUdpBroadcast(context, connectCallBack, true);
        if (isconncting) {
            isconncting = false;
            configUdpBroadcast.stopReceive();
        }
        isconncting = true;
        new Thread(findMoudleThread).start();
    }

    private Runnable findMoudleThread = new Runnable() {
        @Override
        public void run() {
            while (isconncting) {
                configUdpBroadcast.sendFindCmd();
                Log.e("msg", "发送广播");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    };

    /**
     * 销毁
     */
    public void onDestroy() {
        isconncting = false;
        mIsConncting = false;
        if (mSnifferSmartLinker != null) {
            mSnifferSmartLinker.stop();
            mSnifferSmartLinker.setOnSmartLinkListener(null);
            mSnifferSmartLinker = null;
        }
        if (configUdpBroadcast != null) {
            configUdpBroadcast.stopConnection();
            configUdpBroadcast = null;
        }
        joyoungDMS = null;
        System.gc();
    }

}
